import os
from flask import render_template, flash, redirect, url_for, Blueprint, request
from models import app, User, db, Todo
from flask_login import login_required, current_user
from datetime import datetime


bp_todo = Blueprint("bp_todo", __name__)


@bp_todo.route("/", methods=["GET"])
@login_required
def index():
    return redirect(url_for("bp_todo.list_todo"), code=302)


@bp_todo.route("/list", methods=["GET"])
@login_required
def list_todo():
    todos = (
        db.session.query(
            Todo.id, Todo.title, Todo.createtime, Todo.finish, Todo.tag, Todo.edittime
        )
        .filter_by(userid=current_user.id)
        .filter_by(delete=False)
        .order_by(Todo.edittime.desc())
        .order_by(Todo.createtime.desc())
        .all()
    )
    return render_template("todo/list.html", todos=todos)


@bp_todo.route("/trash", methods=["GET"])
@login_required
def list_trash():
    todos = (
        db.session.query(
            Todo.id, Todo.title, Todo.createtime, Todo.finish, Todo.tag, Todo.edittime
        )
        .filter_by(userid=current_user.id)
        .filter_by(delete=True)
        .order_by(Todo.edittime.desc())
        .order_by(Todo.createtime.desc())
        .all()
    )
    return render_template("todo/trash.html", todos=todos)


@bp_todo.route("/add", methods=["POST"])
@login_required
def add():
    userid = current_user.id
    title = request.form.get("todo")
    if title.strip().__len__() == 0:
        pass
        # flash("Nothing Input", category="danger")
    else:
        tag = request.form.get("tag")
        todo = Todo(title=title, userid=userid, createtime=datetime.now(), tag=tag,)
        db.session.add(todo)
        db.session.commit()
        # flash("添加成功", category="success")

    return redirect(url_for("bp_todo.list_todo"), code=302)


@bp_todo.route("/top/<todoid>", methods=["GET"])
@login_required
def top(todoid):
    todo = Todo.query.filter_by(id=todoid).first()
    if not todo:
        flash("Todo 不存在", category="danger")
    else:
        todo.edittime = datetime.now()
        db.session.commit()
    return redirect(url_for("bp_todo.list_todo"), code=302)


@bp_todo.route("/down/<todoid>", methods=["GET"])
@login_required
def down(todoid):
    todo = Todo.query.filter_by(id=todoid).first()
    if not todo:
        flash("Todo 不存在", category="danger")
    else:
        todo.edittime = None
        db.session.commit()
    return redirect(url_for("bp_todo.list_todo"), code=302)


@bp_todo.route("/finish/<todoid>", methods=["GET"])
@login_required
def finish(todoid):
    todo = Todo.query.filter_by(id=todoid).first()
    if not todo:
        flash("Todo 不存在", category="danger")
    else:
        todo.finish = not todo.finish
        db.session.commit()
    return redirect(url_for("bp_todo.list_todo"), code=302)


@bp_todo.route("/delete/<todoid>", methods=["GET"])
@login_required
def delete(todoid):
    todo = Todo.query.filter_by(id=todoid).first()
    if not todo:
        flash("Todo 不存在", category="danger")
    else:
        todo.delete = not todo.delete
        db.session.commit()
    return redirect(url_for("bp_todo.list_todo"), code=302)


if __name__ == "__main__":
    pass
